<?php
class User extends AppModel 
{
    /* validate data enetered by user */
    public $validate = array(
        'username' => array(
			'exists' => array(
				'rule' => array('checkUnique', 'username'),
				'message' => 'Der Benutzername existiert bereits!'
			),
            'required' => array(
                'rule' => array('notEmpty'),
                'message' => 'Ein Benutzername wird benötigt!'
            )
        ),
        'password' => array(
            'required' => array(
                'rule' => array('notEmpty'),
                'message' => 'Ein Passwort wird benötigt!'
            )
        ),
		'email' => array(
			'email' => array(
				'rule' => 'email',
				'message' => 'Keine gültige Email-Adresse!'
			),
			'required' => array(
				'rule' => array('email'),
				'message' => 'Eine Email-Adresse wird benötigt!'
			)
		)
    );
    
    public function beforeSave($options = array()) 
	{    
		/* password hashing */    
		if (isset($this->data[$this->alias]['password'])) 
		{
			$this->data[$this->alias]['password'] = AuthComponent::password($this->data[$this->alias]['password']);
		}
	
		return true;
	}
	
	public function checkUnique( $data, $fieldName ) {
        $valid = false;
        if(isset($fieldName) && $this->hasField($fieldName)) {
            $valid = $this->isUnique(array($fieldName => $data));
        }
        return $valid;
    } 
}